home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / job.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-05  |  59.5 KB  |  2,342 lines

  1. /* Job execution and handling for GNU Make.
  2. Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "job.h"
  21. #include "filedef.h"
  22. #include "commands.h"
  23. #include "variable.h"
  24. #include <assert.h>
  25.  
  26. /* Default shell to use.  */
  27. #ifdef WINDOWS32
  28. char *default_shell = "sh.exe";
  29. int no_default_sh_exe = 1;
  30. #else  /* WINDOWS32 */
  31. #ifdef _AMIGA
  32. char default_shell[] = "";
  33. extern int MyExecute (char **);
  34. #else
  35. #ifdef __MSDOS__
  36. /* The default shell is a pointer so we can change it if Makefile
  37.    says so.  It is without an explicit path so we get a chance
  38.    to search the $PATH for it (since MSDOS doesn't have standard
  39.    directories we could trust).  */
  40. char *default_shell = "command.com";
  41. #else  /* __MSDOS__ */
  42. char default_shell[] = "/bin/sh";
  43. #endif /* __MSDOS__ */
  44. #endif /* _AMIGA */
  45. #endif /* WINDOWS32 */
  46.  
  47. #ifdef __MSDOS__
  48. #include <process.h>
  49. static int execute_by_shell;
  50. static int dos_pid = 123;
  51. int dos_status;
  52. int dos_command_running;
  53. #endif /* __MSDOS__ */
  54.  
  55. #ifdef _AMIGA
  56. #include <proto/dos.h>
  57. static int amiga_pid = 123;
  58. static int amiga_status;
  59. static char amiga_bname[32];
  60. static int amiga_batch_file;
  61. #endif /* Amiga.  */
  62.  
  63. #ifdef VMS
  64. #include <time.h>
  65. #include <processes.h>
  66. #include <starlet.h>
  67. #include <lib$routines.h>
  68. #endif
  69.  
  70. #ifdef WINDOWS32
  71. #include <windows.h>
  72. #include <io.h>
  73. #include <process.h>
  74. #include "sub_proc.h"
  75. #include "w32err.h"
  76. #include "pathstuff.h"
  77.  
  78. /* this stuff used if no sh.exe is around */
  79. static char *dos_bname;
  80. static char *dos_bename;
  81. static int dos_batch_file;
  82. #endif /* WINDOWS32 */
  83.  
  84. #ifdef HAVE_FCNTL_H
  85. #include <fcntl.h>
  86. #else
  87. #include <sys/file.h>
  88. #endif
  89.  
  90. #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
  91. #include <sys/wait.h>
  92. #endif
  93.  
  94. #ifdef    HAVE_WAITPID
  95. #define    WAIT_NOHANG(status)    waitpid (-1, (status), WNOHANG)
  96. #else    /* Don't have waitpid.  */
  97. #ifdef    HAVE_WAIT3
  98. #ifndef    wait3
  99. extern int wait3 ();
  100. #endif
  101. #define    WAIT_NOHANG(status)    wait3 ((status), WNOHANG, (struct rusage *) 0)
  102. #endif    /* Have wait3.  */
  103. #endif    /* Have waitpid.  */
  104.  
  105. #if    !defined (wait) && !defined (POSIX)
  106. extern int wait ();
  107. #endif
  108.  
  109. #ifndef    HAVE_UNION_WAIT
  110.  
  111. #define    WAIT_T int
  112.  
  113. #ifndef    WTERMSIG
  114. #define WTERMSIG(x) ((x) & 0x7f)
  115. #endif
  116. #ifndef    WCOREDUMP
  117. #define WCOREDUMP(x) ((x) & 0x80)
  118. #endif
  119. #ifndef    WEXITSTATUS
  120. #define WEXITSTATUS(x) (((x) >> 8) & 0xff)
  121. #endif
  122. #ifndef    WIFSIGNALED
  123. #define WIFSIGNALED(x) (WTERMSIG (x) != 0)
  124. #endif
  125. #ifndef    WIFEXITED
  126. #define WIFEXITED(x) (WTERMSIG (x) == 0)
  127. #endif
  128.  
  129. #else    /* Have `union wait'.  */
  130.  
  131. #define WAIT_T union wait
  132. #ifndef    WTERMSIG
  133. #define WTERMSIG(x)    ((x).w_termsig)
  134. #endif
  135. #ifndef    WCOREDUMP
  136. #define WCOREDUMP(x)    ((x).w_coredump)
  137. #endif
  138. #ifndef WEXITSTATUS
  139. #define WEXITSTATUS(x)    ((x).w_retcode)
  140. #endif
  141. #ifndef    WIFSIGNALED
  142. #define    WIFSIGNALED(x)    (WTERMSIG(x) != 0)
  143. #endif
  144. #ifndef    WIFEXITED
  145. #define    WIFEXITED(x)    (WTERMSIG(x) == 0)
  146. #endif
  147.  
  148. #endif    /* Don't have `union wait'.  */
  149.  
  150. #ifdef VMS
  151. static int vms_jobsefnmask=0;
  152. #endif /* !VMS */
  153.  
  154. #ifndef    HAVE_UNISTD_H
  155. extern int dup2 ();
  156. extern int execve ();
  157. extern void _exit ();
  158. #ifndef VMS
  159. extern int geteuid ();
  160. extern int getegid ();
  161. extern int setgid ();
  162. extern int getgid ();
  163. #endif
  164. #endif
  165.  
  166. extern char *allocated_variable_expand_for_file PARAMS ((char *line, struct file *file));
  167.  
  168. extern int getloadavg PARAMS ((double loadavg[], int nelem));
  169. extern int start_remote_job PARAMS ((char **argv, char **envp, int stdin_fd,
  170.         int *is_remote, int *id_ptr, int *used_stdin));
  171. extern int start_remote_job_p PARAMS ((void));
  172. extern int remote_status PARAMS ((int *exit_code_ptr, int *signal_ptr,
  173.         int *coredump_ptr, int block));
  174.  
  175. RETSIGTYPE child_handler PARAMS ((int));
  176. static void free_child PARAMS ((struct child *));
  177. static void start_job_command PARAMS ((struct child *child));
  178. static int load_too_high PARAMS ((void));
  179. static int job_next_command PARAMS ((struct child *));
  180. static int start_waiting_job PARAMS ((struct child *));
  181. #ifdef VMS
  182. static void vmsWaitForChildren PARAMS ((int *));
  183. #endif
  184.  
  185. /* Chain of all live (or recently deceased) children.  */
  186.  
  187. struct child *children = 0;
  188.  
  189. /* Number of children currently running.  */
  190.  
  191. unsigned int job_slots_used = 0;
  192.  
  193. /* Nonzero if the `good' standard input is in use.  */
  194.  
  195. static int good_stdin_used = 0;
  196.  
  197. /* Chain of children waiting to run until the load average goes down.  */
  198.  
  199. static struct child *waiting_jobs = 0;
  200.  
  201. /* Non-zero if we use a *real* shell (always so on Unix).  */
  202.  
  203. int unixy_shell = 1;
  204.  
  205. #ifdef WINDOWS32
  206. /*
  207.  * The macro which references this function is defined in make.h.
  208.  */
  209. int w32_kill(int pid, int sig)
  210. {
  211.        return ((process_kill(pid, sig) == TRUE) ? 0 : -1);
  212. }
  213. #endif /* WINDOWS32 */
  214.  
  215. /* Write an error message describing the exit status given in
  216.    EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
  217.    Append "(ignored)" if IGNORED is nonzero.  */
  218.  
  219. static void
  220. child_error (target_name, exit_code, exit_sig, coredump, ignored)
  221.      char *target_name;
  222.      int exit_code, exit_sig, coredump;
  223.      int ignored;
  224. {
  225.   if (ignored && silent_flag)
  226.     return;
  227.  
  228. #ifdef VMS
  229.   if (!(exit_code & 1))
  230.       error("*** [%s] Error 0x%x%s", target_name, exit_code, ((ignored)? " (ignored)" : ""));
  231. #else
  232.   if (exit_sig == 0)
  233.     error (ignored ? "[%s] Error %d (ignored)" :
  234.        "*** [%s] Error %d",
  235.        target_name, exit_code);
  236.   else
  237.     error ("*** [%s] %s%s",
  238.        target_name, strsignal (exit_sig),
  239.        coredump ? " (core dumped)" : "");
  240. #endif /* VMS */
  241. }
  242.  
  243. static unsigned int dead_children = 0;
  244.  
  245. #ifdef VMS
  246. /* Wait for nchildren children to terminate */
  247. static void
  248. vmsWaitForChildren(int *status)
  249. {
  250.   while (1)
  251.     {
  252.       if (!vms_jobsefnmask)
  253.     {
  254.       *status = 0;
  255.       return;
  256.     }
  257.  
  258.       *status = sys$wflor (32, vms_jobsefnmask);
  259.     }
  260.   return;
  261. }
  262. #endif
  263.  
  264.  
  265. /* Notice that a child died.
  266.    reap_children should be called when convenient.  */
  267. RETSIGTYPE
  268. child_handler (sig)
  269.      int sig;
  270. {
  271.   ++dead_children;
  272.  
  273.   if (debug_flag)
  274.     printf ("Got a SIGCHLD; %d unreaped children.\n", dead_children);
  275. }
  276.  
  277. extern int shell_function_pid, shell_function_completed;
  278.  
  279. /* Reap dead children, storing the returned status and the new command
  280.    state (`cs_finished') in the `file' member of the `struct child' for the
  281.    dead child, and removing the child from the chain.  If BLOCK nonzero,
  282.    reap at least one child, waiting for it to die if necessary.  If ERR is
  283.    nonzero, print an error message first.  */
  284.  
  285. void
  286. reap_children (block, err)
  287.      int block, err;
  288. {
  289.   WAIT_T status;
  290.  
  291.   while ((children != 0 || shell_function_pid != 0) &&
  292.      (block || dead_children > 0))
  293.     {
  294.       int remote = 0;
  295.       register int pid;
  296.       int exit_code, exit_sig, coredump;
  297.       register struct child *lastc, *c;
  298.       int child_failed;
  299.       int any_remote, any_local;
  300.  
  301.       if (err && dead_children == 0)
  302.     {
  303.       /* We might block for a while, so let the user know why.  */
  304.       fflush (stdout);
  305.       error ("*** Waiting for unfinished jobs....");
  306.     }
  307.  
  308.       /* We have one less dead child to reap.
  309.      The test and decrement are not atomic; if it is compiled into:
  310.          register = dead_children - 1;
  311.         dead_children = register;
  312.      a SIGCHLD could come between the two instructions.
  313.      child_handler increments dead_children.
  314.      The second instruction here would lose that increment.  But the
  315.      only effect of dead_children being wrong is that we might wait
  316.      longer than necessary to reap a child, and lose some parallelism;
  317.      and we might print the "Waiting for unfinished jobs" message above
  318.      when not necessary.  */
  319.  
  320.       if (dead_children > 0)
  321.     --dead_children;
  322.  
  323.       any_remote = 0;
  324.       any_local = shell_function_pid != 0;
  325.       for (c = children; c != 0; c = c->next)
  326.     {
  327.       any_remote |= c->remote;
  328.       any_local |= ! c->remote;
  329.       if (debug_flag)
  330.         printf ("Live child 0x%08lx PID %d%s\n",
  331.             (unsigned long int) c,
  332.             c->pid, c->remote ? " (remote)" : "");
  333. #ifdef VMS
  334.       break;
  335. #endif
  336.     }
  337.  
  338.       /* First, check for remote children.  */
  339.       if (any_remote)
  340.     pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
  341.       else
  342.     pid = 0;
  343.  
  344.       if (pid < 0)
  345.     {
  346.     remote_status_lose:
  347. #ifdef    EINTR
  348.       if (errno == EINTR)
  349.         continue;
  350. #endif
  351.       pfatal_with_name ("remote_status");
  352.     }
  353.       else if (pid == 0)
  354.     {
  355. #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
  356.       /* No remote children.  Check for local children.  */
  357.  
  358.       if (any_local)
  359.         {
  360. #ifdef VMS
  361.           vmsWaitForChildren (&status);
  362.           pid = c->pid;
  363. #else
  364. #ifdef WAIT_NOHANG
  365.           if (!block)
  366.         pid = WAIT_NOHANG (&status);
  367.           else
  368. #endif
  369.         pid = wait (&status);
  370. #endif /* !VMS */
  371.         }
  372.       else
  373.         pid = 0;
  374.  
  375.       if (pid < 0)
  376.         {
  377. #ifdef EINTR
  378.           if (errno == EINTR)
  379.         continue;
  380. #endif
  381.           pfatal_with_name ("wait");
  382.         }
  383.       else if (pid == 0)
  384.         {
  385.           /* No local children.  */
  386.           if (block && any_remote)
  387.         {
  388.           /* Now try a blocking wait for a remote child.  */
  389.           pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
  390.           if (pid < 0)
  391.             goto remote_status_lose;
  392.           else if (pid == 0)
  393.             /* No remote children either.  Finally give up.  */
  394.             break;
  395.           else
  396.             /* We got a remote child.  */
  397.             remote = 1;
  398.         }
  399.           else
  400.         break;
  401.         }
  402.       else
  403.         {
  404.           /* Chop the status word up.  */
  405.           exit_code = WEXITSTATUS (status);
  406.           exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
  407.           coredump = WCOREDUMP (status);
  408.         }
  409. #else    /* __MSDOS__, Amiga, WINDOWS32.  */
  410. #ifdef __MSDOS__
  411.       /* Life is very different on MSDOS.  */
  412.       pid = dos_pid - 1;
  413.       status = dos_status;
  414.       exit_code = WEXITSTATUS (status);
  415.       if (exit_code == 0xff)
  416.         exit_code = -1;
  417.       exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
  418.       coredump = 0;
  419. #endif /* __MSDOS__ */
  420. #ifdef _AMIGA
  421.       /* Same on Amiga */
  422.       pid = amiga_pid - 1;
  423.       status = amiga_status;
  424.       exit_code = amiga_status;
  425.       exit_sig = 0;
  426.       coredump = 0;
  427. #endif /* _AMIGA */
  428. #ifdef WINDOWS32
  429.          {
  430.            HANDLE hPID;
  431.            int err;
  432.  
  433.            /* wait for anything to finish */
  434.            if (hPID = process_wait_for_any()) {
  435.  
  436.              /* was an error found on this process? */
  437.              err = process_last_err(hPID);
  438.  
  439.              /* get exit data */
  440.              exit_code = process_exit_code(hPID);
  441.  
  442.              if (err)
  443.                fprintf(stderr, "make (e=%d): %s",
  444.                        exit_code, map_windows32_error_to_string(exit_code));
  445.  
  446.              exit_sig = process_signal(hPID);
  447.  
  448.              /* cleanup process */
  449.              process_cleanup(hPID);
  450.  
  451.              if (dos_batch_file) {
  452.                remove (dos_bname);
  453.                remove (dos_bename);
  454.                dos_batch_file = 0;
  455.              }
  456.  
  457.              coredump = 0;
  458.            }
  459.            pid = (int) hPID;
  460.          }
  461. #endif /* WINDOWS32 */
  462. #endif    /* Not __MSDOS__ */
  463.     }
  464.       else
  465.     /* We got a remote child.  */
  466.     remote = 1;
  467.  
  468.       /* Check if this is the child of the `shell' function.  */
  469.       if (!remote && pid == shell_function_pid)
  470.     {
  471.       /* It is.  Leave an indicator for the `shell' function.  */
  472.       if (exit_sig == 0 && exit_code == 127)
  473.         shell_function_completed = -1;
  474.       else
  475.         shell_function_completed = 1;
  476.       break;
  477.     }
  478.  
  479.       child_failed = exit_sig != 0 || exit_code != 0;
  480.  
  481.       /* Search for a child matching the deceased one.  */
  482.       lastc = 0;
  483.       for (c = children; c != 0; lastc = c, c = c->next)
  484.     if (c->remote == remote && c->pid == pid)
  485.       break;
  486.  
  487.       if (c == 0)
  488.     {
  489.       /* An unknown child died.  */
  490.       char buf[100];
  491.       sprintf (buf, "Unknown%s job %d", remote ? " remote" : "", pid);
  492.       if (child_failed)
  493.         child_error (buf, exit_code, exit_sig, coredump,
  494.              ignore_errors_flag);
  495.       else
  496.         error ("%s finished.", buf);
  497.     }
  498.       else
  499.     {
  500.       if (debug_flag)
  501.         printf ("Reaping %s child 0x%08lx PID %d%s\n",
  502.             child_failed ? "losing" : "winning",
  503.             (unsigned long int) c,
  504.             c->pid, c->remote ? " (remote)" : "");
  505.  
  506.       /* If this child had the good stdin, say it is now free.  */
  507.       if (c->good_stdin)
  508.         good_stdin_used = 0;
  509.  
  510.       if (child_failed && !c->noerror && !ignore_errors_flag)
  511.         {
  512.           /* The commands failed.  Write an error message,
  513.          delete non-precious targets, and abort.  */
  514.           static int delete_on_error = -1;
  515.           child_error (c->file->name, exit_code, exit_sig, coredump, 0);
  516.           c->file->update_status = 2;
  517.           if (delete_on_error == -1)
  518.         {
  519.           struct file *f = lookup_file (".DELETE_ON_ERROR");
  520.           delete_on_error = f != 0 && f->is_target;
  521.         }
  522.           if (exit_sig != 0 || delete_on_error)
  523.         delete_child_targets (c);
  524.         }
  525.       else
  526.         {
  527.           if (child_failed)
  528.         {
  529.           /* The commands failed, but we don't care.  */
  530.           child_error (c->file->name,
  531.                    exit_code, exit_sig, coredump, 1);
  532.           child_failed = 0;
  533.         }
  534.  
  535.           /* If there are more commands to run, try to start them.  */
  536.           if (job_next_command (c))
  537.         {
  538.           if (handling_fatal_signal)
  539.             {
  540.               /* Never start new commands while we are dying.
  541.              Since there are more commands that wanted to be run,
  542.              the target was not completely remade.  So we treat
  543.              this as if a command had failed.  */
  544.               c->file->update_status = 2;
  545.             }
  546.           else
  547.             {
  548.               /* Check again whether to start remotely.
  549.              Whether or not we want to changes over time.
  550.              Also, start_remote_job may need state set up
  551.              by start_remote_job_p.  */
  552.               c->remote = start_remote_job_p ();
  553.               start_job_command (c);
  554.               /* Fatal signals are left blocked in case we were
  555.              about to put that child on the chain.  But it is
  556.              already there, so it is safe for a fatal signal to
  557.              arrive now; it will clean up this child's targets.  */
  558.               unblock_sigs ();
  559.               if (c->file->command_state == cs_running)
  560.             /* We successfully started the new command.
  561.                Loop to reap more children.  */
  562.             continue;
  563.             }
  564.  
  565.           if (c->file->update_status != 0)
  566.             /* We failed to start the commands.  */
  567.             delete_child_targets (c);
  568.         }
  569.           else
  570.         /* There are no more commands.  We got through them all
  571.            without an unignored error.  Now the target has been
  572.            successfully updated.  */
  573.         c->file->update_status = 0;
  574.         }
  575.  
  576.       /* When we get here, all the commands for C->file are finished
  577.          (or aborted) and C->file->update_status contains 0 or 2.  But
  578.          C->file->command_state is still cs_running if all the commands
  579.          ran; notice_finish_file looks for cs_running to tell it that
  580.          it's interesting to check the file's modtime again now.  */
  581.  
  582.       if (! handling_fatal_signal)
  583.         /* Notice if the target of the commands has been changed.
  584.            This also propagates its values for command_state and
  585.            update_status to its also_make files.  */
  586.         notice_finished_file (c->file);
  587.  
  588.       if (debug_flag)
  589.         printf ("Removing child 0x%08lx PID %d%s from chain.\n",
  590.             (unsigned long int) c,
  591.             c->pid, c->remote ? " (remote)" : "");
  592.  
  593.       /* Block fatal signals while frobnicating the list, so that
  594.          children and job_slots_used are always consistent.  Otherwise
  595.          a fatal signal arriving after the child is off the chain and
  596.          before job_slots_used is decremented would believe a child was
  597.          live and call reap_children again.  */
  598.       block_sigs ();
  599.  
  600.       /* Remove the child from the chain and free it.  */
  601.       if (lastc == 0)
  602.         children = c->next;
  603.       else
  604.         lastc->next = c->next;
  605.       if (! handling_fatal_signal) /* Don't bother if about to die.  */
  606.         free_child (c);
  607.  
  608.       /* There is now another slot open.  */
  609.       if (job_slots_used > 0)
  610.         --job_slots_used;
  611.  
  612.       unblock_sigs ();
  613.  
  614.       /* If the job failed, and the -k flag was not given, die,
  615.          unless we are already in the process of dying.  */
  616.       if (!err && child_failed && !keep_going_flag &&
  617.           /* fatal_error_signal will die with the right signal.  */
  618.           !handling_fatal_signal)
  619.         die (2);
  620.     }
  621.  
  622.       /* Only block for one child.  */
  623.       block = 0;
  624.     }
  625.   return;
  626. }
  627.  
  628. /* Free the storage allocated for CHILD.  */
  629.  
  630. static void
  631. free_child (child)
  632.      register struct child *child;
  633. {
  634.   if (child->command_lines != 0)
  635.     {
  636.       register unsigned int i;
  637.       for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
  638.     free (child->command_lines[i]);
  639.       free ((char *) child->command_lines);
  640.     }
  641.  
  642.   if (child->environment != 0)
  643.     {
  644.       register char **ep = child->environment;
  645.       while (*ep != 0)
  646.     free (*ep++);
  647.       free ((char *) child->environment);
  648.     }
  649.  
  650.   free ((char *) child);
  651. }
  652.  
  653. #ifdef POSIX
  654. extern sigset_t fatal_signal_set;
  655. #endif
  656.  
  657. void
  658. block_sigs ()
  659. {
  660. #ifdef     POSIX
  661.   (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
  662. #else
  663. #ifdef    HAVE_SIGSETMASK
  664.   (void) sigblock (fatal_signal_mask);
  665. #endif
  666. #endif
  667. }
  668.  
  669. #ifdef    POSIX
  670. void
  671. unblock_sigs ()
  672. {
  673.   sigset_t empty;
  674.   sigemptyset (&empty);
  675.   sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
  676. }
  677. #endif
  678.  
  679. /* Start a job to run the commands specified in CHILD.
  680.    CHILD is updated to reflect the commands and ID of the child process.
  681.  
  682.    NOTE: On return fatal signals are blocked!  The caller is responsible
  683.    for calling `unblock_sigs', once the new child is safely on the chain so
  684.    it can be cleaned up in the event of a fatal signal.  */
  685.  
  686. static void
  687. start_job_command (child)
  688.      register struct child *child;
  689. {
  690. #ifndef _AMIGA
  691.   static int bad_stdin = -1;
  692. #endif
  693.   register char *p;
  694.   int flags;
  695. #ifdef VMS
  696.   char *argv;
  697. #else
  698.   char **argv;
  699. #endif
  700.  
  701.   /* Combine the flags parsed for the line itself with
  702.      the flags specified globally for this target.  */
  703.   flags = (child->file->command_flags
  704.        | child->file->cmds->lines_flags[child->command_line - 1]);
  705.  
  706.   p = child->command_ptr;
  707.   child->noerror = flags & COMMANDS_NOERROR;
  708.  
  709.   while (*p != '\0')
  710.     {
  711.       if (*p == '@')
  712.     flags |= COMMANDS_SILENT;
  713.       else if (*p == '+')
  714.     flags |= COMMANDS_RECURSE;
  715.       else if (*p == '-')
  716.     child->noerror = 1;
  717.       else if (!isblank (*p) && *p != '+')
  718.     break;
  719.       ++p;
  720.     }
  721.  
  722.   /* If -q was given, just say that updating `failed'.  The exit status of
  723.      1 tells the user that -q is saying `something to do'; the exit status
  724.      for a random error is 2.  */
  725.   if (question_flag && !(flags & COMMANDS_RECURSE))
  726.     {
  727.       child->file->update_status = 1;
  728.       notice_finished_file (child->file);
  729.       return;
  730.     }
  731.  
  732.   /* There may be some preceding whitespace left if there
  733.      was nothing but a backslash on the first line.  */
  734.   p = next_token (p);
  735.  
  736.   /* Figure out an argument list from this command line.  */
  737.  
  738.   {
  739.     char *end = 0;
  740. #ifdef VMS
  741.     argv = p;
  742. #else
  743.     argv = construct_command_argv (p, &end, child->file);
  744. #endif
  745.     if (end == NULL)
  746.       child->command_ptr = NULL;
  747.     else
  748.       {
  749.     *end++ = '\0';
  750.     child->command_ptr = end;
  751.       }
  752.   }
  753.  
  754.   if (touch_flag && !(flags & COMMANDS_RECURSE))
  755.     {
  756.       /* Go on to the next command.  It might be the recursive one.
  757.      We construct ARGV only to find the end of the command line.  */
  758. #ifndef VMS
  759.       free (argv[0]);
  760.       free ((char *) argv);
  761. #endif
  762.       argv = 0;
  763.     }
  764.  
  765.   if (argv == 0)
  766.     {
  767.     next_command:
  768.       /* This line has no commands.  Go to the next.  */
  769.       if (job_next_command (child))
  770.     start_job_command (child);
  771.       else
  772.     {
  773.       /* No more commands.  All done.  */
  774.       child->file->update_status = 0;
  775.       notice_finished_file (child->file);
  776.     }
  777.       return;
  778.     }
  779.  
  780.   /* Print out the command.  If silent, we call `message' with null so it
  781.      can log the working directory before the command's own error messages
  782.      appear.  */
  783.  
  784.   message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
  785.        ? "%s" : (char *) 0, p);
  786.  
  787.   /* Optimize an empty command.  People use this for timestamp rules,
  788.      and forking a useless shell all the time leads to inefficiency. */
  789.  
  790. #if !defined(VMS) && !defined(_AMIGA)
  791.   if (
  792. #ifdef __MSDOS__
  793.       unixy_shell    /* the test is complicated and we already did it */
  794. #else
  795.       (argv[0] && !strcmp(argv[0], "/bin/sh"))
  796. #endif
  797.       && (argv[1]   && !strcmp(argv[1], "-c"))
  798.       && (argv[2]   && !strcmp(argv[2], ":"))
  799.       && argv[3] == NULL)
  800.     {
  801.       set_command_state (child->file, cs_running);
  802.       goto next_command;
  803.     }
  804. #endif  /* !VMS && !_AMIGA */
  805.  
  806.   /* Tell update_goal_chain that a command has been started on behalf of
  807.      this target.  It is important that this happens here and not in
  808.      reap_children (where we used to do it), because reap_children might be
  809.      reaping children from a different target.  We want this increment to
  810.      guaranteedly indicate that a command was started for the dependency
  811.      chain (i.e., update_file recursion chain) we are processing.  */
  812.  
  813.   ++commands_started;
  814.  
  815.   /* If -n was given, recurse to get the next line in the sequence.  */
  816.  
  817.   if (just_print_flag && !(flags & COMMANDS_RECURSE))
  818.     {
  819. #ifndef VMS
  820.       free (argv[0]);
  821.       free ((char *) argv);
  822. #endif
  823.       goto next_command;
  824.     }
  825.  
  826.   /* Flush the output streams so they won't have things written twice.  */
  827.  
  828.   fflush (stdout);
  829.   fflush (stderr);
  830.  
  831. #ifndef VMS
  832. #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
  833.  
  834.   /* Set up a bad standard input that reads from a broken pipe.  */
  835.  
  836.   if (bad_stdin == -1)
  837.     {
  838.       /* Make a file descriptor that is the read end of a broken pipe.
  839.      This will be used for some children's standard inputs.  */
  840.       int pd[2];
  841.       if (pipe (pd) == 0)
  842.     {
  843.       /* Close the write side.  */
  844.       (void) close (pd[1]);
  845.       /* Save the read side.  */
  846.       bad_stdin = pd[0];
  847.  
  848.       /* Set the descriptor to close on exec, so it does not litter any
  849.          child's descriptor table.  When it is dup2'd onto descriptor 0,
  850.          that descriptor will not close on exec.  */
  851. #ifdef FD_SETFD
  852. #ifndef FD_CLOEXEC
  853. #define FD_CLOEXEC 1
  854. #endif
  855.       (void) fcntl (bad_stdin, F_SETFD, FD_CLOEXEC);
  856. #endif
  857.     }
  858.     }
  859.  
  860. #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
  861.  
  862.   /* Decide whether to give this child the `good' standard input
  863.      (one that points to the terminal or whatever), or the `bad' one
  864.      that points to the read side of a broken pipe.  */
  865.  
  866.   child->good_stdin = !good_stdin_used;
  867.   if (child->good_stdin)
  868.     good_stdin_used = 1;
  869.  
  870. #endif /* !VMS */
  871.  
  872.   child->deleted = 0;
  873.  
  874. #ifndef _AMIGA
  875.   /* Set up the environment for the child.  */
  876.   if (child->environment == 0)
  877.     child->environment = target_environment (child->file);
  878. #endif
  879.  
  880. #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
  881.  
  882. #ifndef VMS
  883.   /* start_waiting_job has set CHILD->remote if we can start a remote job.  */
  884.   if (child->remote)
  885.     {
  886.       int is_remote, id, used_stdin;
  887.       if (start_remote_job (argv, child->environment,
  888.                 child->good_stdin ? 0 : bad_stdin,
  889.                 &is_remote, &id, &used_stdin))
  890.     goto error;
  891.       else
  892.     {
  893.       if (child->good_stdin && !used_stdin)
  894.         {
  895.           child->good_stdin = 0;
  896.           good_stdin_used = 0;
  897.         }
  898.       child->remote = is_remote;
  899.       child->pid = id;
  900.     }
  901.     }
  902.   else
  903. #endif /* !VMS */
  904.     {
  905.       /* Fork the child process.  */
  906.  
  907.       char **parent_environ;
  908.  
  909.       block_sigs ();
  910.  
  911.       child->remote = 0;
  912.  
  913. #ifdef VMS
  914.  
  915.       if (!child_execute_job (argv, child)) {
  916.         /* Fork failed!  */
  917.         perror_with_name ("vfork", "");
  918.         goto error;
  919.       }
  920.  
  921. #else
  922.  
  923.       parent_environ = environ;
  924.       child->pid = vfork ();
  925.       environ = parent_environ;    /* Restore value child may have clobbered.  */
  926.       if (child->pid == 0)
  927.     {
  928.       /* We are the child side.  */
  929.       unblock_sigs ();
  930.       child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
  931.                  argv, child->environment);
  932.     }
  933.       else if (child->pid < 0)
  934.     {
  935.       /* Fork failed!  */
  936.       unblock_sigs ();
  937.       perror_with_name ("vfork", "");
  938.       goto error;
  939.     }
  940. #endif /* !VMS */
  941.     }
  942.  
  943. #else    /* __MSDOS__ or Amiga or WINDOWS32 */
  944. #ifdef __MSDOS__
  945.   {
  946.     int proc_return;
  947.  
  948.     block_sigs ();
  949.     dos_status = 0;
  950.  
  951.     /* We call `system' to do the job of the SHELL, since stock DOS
  952.        shell is too dumb.  Our `system' knows how to handle long
  953.        command lines even if pipes/redirection is needed; it will only
  954.        call COMMAND.COM when its internal commands are used.  */
  955.     if (execute_by_shell)
  956.       {
  957.     char *cmdline = argv[0];
  958.     /* We don't have a way to pass environment to `system',
  959.        so we need to save and restore ours, sigh...  */
  960.     char **parent_environ = environ;
  961.  
  962.     environ = child->environment;
  963.  
  964.     /* If we have a *real* shell, tell `system' to call
  965.        it to do everything for us.  */
  966.     if (unixy_shell)
  967.       {
  968.         /* A *real* shell on MSDOS may not support long
  969.            command lines the DJGPP way, so we must use `system'.  */
  970.         cmdline = argv[2];    /* get past "shell -c" */
  971.       }
  972.  
  973.     dos_command_running = 1;
  974.     proc_return = system (cmdline);
  975.     dos_command_running = 0;
  976.     environ = parent_environ;
  977.     execute_by_shell = 0;    /* for the next time */
  978.       }
  979.     else
  980.       {
  981.     dos_command_running = 1;
  982.     proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
  983.     dos_command_running = 0;
  984.       }
  985.  
  986.     if (proc_return == -1)
  987.       dos_status |= 0xff;
  988.     else
  989.       dos_status |= (proc_return & 0xff);
  990.     ++dead_children;
  991.     child->pid = dos_pid++;
  992.   }
  993. #endif /* __MSDOS__ */
  994. #ifdef _AMIGA
  995.   amiga_status = MyExecute (argv);
  996.  
  997.   ++dead_children;
  998.   child->pid = amiga_pid++;
  999.   if (amiga_batch_file)
  1000.   {
  1001.      amiga_batch_file = 0;
  1002.      DeleteFile (amiga_bname);        /* Ignore errors.  */
  1003.   }
  1004. #endif    /* Amiga */
  1005. #ifdef WINDOWS32
  1006.   {
  1007.       HANDLE hPID;
  1008.       char* arg0;
  1009.  
  1010.       /* make UNC paths safe for CreateProcess -- backslash format */
  1011.       arg0 = argv[0];
  1012.       if (arg0 && arg0[0] == '/' && arg0[1] == '/')
  1013.         for ( ; arg0 && *arg0; arg0++)
  1014.           if (*arg0 == '/')
  1015.             *arg0 = '\\';
  1016.  
  1017.       /* make sure CreateProcess() has Path it needs */
  1018.       sync_Path_environment();
  1019.  
  1020.       hPID = process_easy(argv, child->environment);
  1021.  
  1022.       if (hPID != INVALID_HANDLE_VALUE)
  1023.         child->pid = (int) hPID;
  1024.       else {
  1025.         int i;
  1026.         unblock_sigs();
  1027.         fprintf(stderr,
  1028.           "process_easy() failed failed to launch process (e=%d)\n",
  1029.           process_last_err(hPID));
  1030.                for (i = 0; argv[i]; i++)
  1031.                  fprintf(stderr, "%s ", argv[i]);
  1032.                fprintf(stderr, "\nCounted %d args in failed launch\n", i);
  1033.       }
  1034.   }
  1035. #endif /* WINDOWS32 */
  1036. #endif    /* __MSDOS__ or Amiga or WINDOWS32 */
  1037.  
  1038.   /* We are the parent side.  Set the state to
  1039.      say the commands are running and return.  */
  1040.  
  1041.   set_command_state (child->file, cs_running);
  1042.  
  1043.   /* Free the storage used by the child's argument list.  */
  1044. #ifndef VMS
  1045.   free (argv[0]);
  1046.   free ((char *) argv);
  1047. #endif
  1048.  
  1049.   return;
  1050.  
  1051.  error:
  1052.   child->file->update_status = 2;
  1053.   notice_finished_file (child->file);
  1054.   return;
  1055. }
  1056.  
  1057. /* Try to start a child running.
  1058.    Returns nonzero if the child was started (and maybe finished), or zero if
  1059.    the load was too high and the child was put on the `waiting_jobs' chain.  */
  1060.  
  1061. static int
  1062. start_waiting_job (c)
  1063.      struct child *c;
  1064. {
  1065.   /* If we can start a job remotely, we always want to, and don't care about
  1066.      the local load average.  We record that the job should be started
  1067.      remotely in C->remote for start_job_command to test.  */
  1068.  
  1069.   c->remote = start_remote_job_p ();
  1070.  
  1071.   /* If this job is to be started locally, and we are already running
  1072.      some jobs, make this one wait if the load average is too high.  */
  1073.   if (!c->remote && job_slots_used > 0 && load_too_high ())
  1074.     {
  1075.       /* Put this child on the chain of children waiting
  1076.      for the load average to go down.  */
  1077.       set_command_state (c->file, cs_running);
  1078.       c->next = waiting_jobs;
  1079.       waiting_jobs = c;
  1080.       return 0;
  1081.     }
  1082.  
  1083.   /* Start the first command; reap_children will run later command lines.  */
  1084.   start_job_command (c);
  1085.  
  1086.   switch (c->file->command_state)
  1087.     {
  1088.     case cs_running:
  1089.       c->next = children;
  1090.       if (debug_flag)
  1091.     printf ("Putting child 0x%08lx PID %05d%s on the chain.\n",
  1092.         (unsigned long int) c,
  1093.         c->pid, c->remote ? " (remote)" : "");
  1094.       children = c;
  1095.       /* One more job slot is in use.  */
  1096.       ++job_slots_used;
  1097.       unblock_sigs ();
  1098.       break;
  1099.  
  1100.     case cs_not_started:
  1101.       /* All the command lines turned out to be empty.  */
  1102.       c->file->update_status = 0;
  1103.       /* FALLTHROUGH */
  1104.  
  1105.     case cs_finished:
  1106.       notice_finished_file (c->file);
  1107.       free_child (c);
  1108.       break;
  1109.  
  1110.     default:
  1111.       assert (c->file->command_state == cs_finished);
  1112.       break;
  1113.     }
  1114.  
  1115.   return 1;
  1116. }
  1117.  
  1118. /* Create a `struct child' for FILE and start its commands running.  */
  1119.  
  1120. void
  1121. new_job (file)
  1122.      register struct file *file;
  1123. {
  1124.   register struct commands *cmds = file->cmds;
  1125.   register struct child *c;
  1126.   char **lines;
  1127.   register unsigned int i;
  1128.  
  1129.   /* Let any previously decided-upon jobs that are waiting
  1130.      for the load to go down start before this new one.  */
  1131.   start_waiting_jobs ();
  1132.  
  1133.   /* Reap any children that might have finished recently.  */
  1134.   reap_children (0, 0);
  1135.  
  1136.   /* Chop the commands up into lines if they aren't already.  */
  1137.   chop_commands (cmds);
  1138.  
  1139.   if (job_slots != 0)
  1140.     /* Wait for a job slot to be freed up.  */
  1141.     while (job_slots_used == job_slots)
  1142.       reap_children (1, 0);
  1143.  
  1144.   /* Expand the command lines and store the results in LINES.  */
  1145.   lines = (char **) xmalloc (cmds->ncommand_lines * sizeof (char *));
  1146.   for (i = 0; i < cmds->ncommand_lines; ++i)
  1147.     {
  1148.       /* Collapse backslash-newline combinations that are inside variable
  1149.      or function references.  These are left alone by the parser so
  1150.      that they will appear in the echoing of commands (where they look
  1151.      nice); and collapsed by construct_command_argv when it tokenizes.
  1152.      But letting them survive inside function invocations loses because
  1153.      we don't want the functions to see them as part of the text.  */
  1154.  
  1155.       char *in, *out, *ref;
  1156.  
  1157.       /* IN points to where in the line we are scanning.
  1158.      OUT points to where in the line we are writing.
  1159.      When we collapse a backslash-newline combination,
  1160.      IN gets ahead of OUT.  */
  1161.  
  1162.       in = out = cmds->command_lines[i];
  1163.       while ((ref = index (in, '$')) != 0)
  1164.     {
  1165.       ++ref;        /* Move past the $.  */
  1166.  
  1167.       if (out != in)
  1168.         /* Copy the text between the end of the last chunk
  1169.            we processed (where IN points) and the new chunk
  1170.            we are about to process (where REF points).  */
  1171.         bcopy (in, out, ref - in);
  1172.  
  1173.       /* Move both pointers past the boring stuff.  */
  1174.       out += ref - in;
  1175.       in = ref;
  1176.  
  1177.       if (*ref == '(' || *ref == '{')
  1178.         {
  1179.           char openparen = *ref;
  1180.           char closeparen = openparen == '(' ? ')' : '}';
  1181.           int count;
  1182.           char *p;
  1183.  
  1184.           *out++ = *in++;    /* Copy OPENPAREN.  */
  1185.           /* IN now points past the opening paren or brace.
  1186.          Count parens or braces until it is matched.  */
  1187.           count = 0;
  1188.           while (*in != '\0')
  1189.         {
  1190.           if (*in == closeparen && --count < 0)
  1191.             break;
  1192.           else if (*in == '\\' && in[1] == '\n')
  1193.             {
  1194.               /* We have found a backslash-newline inside a
  1195.              variable or function reference.  Eat it and
  1196.              any following whitespace.  */
  1197.  
  1198.               int quoted = 0;
  1199.               for (p = in - 1; p > ref && *p == '\\'; --p)
  1200.             quoted = !quoted;
  1201.  
  1202.               if (quoted)
  1203.             /* There were two or more backslashes, so this is
  1204.                not really a continuation line.  We don't collapse
  1205.                the quoting backslashes here as is done in
  1206.                collapse_continuations, because the line will
  1207.                be collapsed again after expansion.  */
  1208.             *out++ = *in++;
  1209.               else
  1210.             {
  1211.               /* Skip the backslash, newline and
  1212.                  any following whitespace.  */
  1213.               in = next_token (in + 2);
  1214.  
  1215.               /* Discard any preceding whitespace that has
  1216.                  already been written to the output.  */
  1217.               while (out > ref && isblank (out[-1]))
  1218.                 --out;
  1219.  
  1220.               /* Replace it all with a single space.  */
  1221.               *out++ = ' ';
  1222.             }
  1223.             }
  1224.           else
  1225.             {
  1226.               if (*in == openparen)
  1227.             ++count;
  1228.  
  1229.               *out++ = *in++;
  1230.             }
  1231.         }
  1232.         }
  1233.     }
  1234.  
  1235.       /* There are no more references in this line to worry about.
  1236.      Copy the remaining uninteresting text to the output.  */
  1237.       if (out != in)
  1238.     strcpy (out, in);
  1239.  
  1240.       /* Finally, expand the line.  */
  1241.       lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
  1242.                              file);
  1243.     }
  1244.  
  1245.   /* Start the command sequence, record it in a new
  1246.      `struct child', and add that to the chain.  */
  1247.  
  1248.   c = (struct child *) xmalloc (sizeof (struct child));
  1249.   c->file = file;
  1250.   c->command_lines = lines;
  1251.   c->command_line = 0;
  1252.   c->command_ptr = 0;
  1253.   c->environment = 0;
  1254.  
  1255.   /* Fetch the first command line to be run.  */
  1256.   job_next_command (c);
  1257.  
  1258.   /* The job is now primed.  Start it running.
  1259.      (This will notice if there are in fact no commands.)  */
  1260.   (void)start_waiting_job (c);
  1261.  
  1262.   if (job_slots == 1)
  1263.     /* Since there is only one job slot, make things run linearly.
  1264.        Wait for the child to die, setting the state to `cs_finished'.  */
  1265.     while (file->command_state == cs_running)
  1266.       reap_children (1, 0);
  1267.  
  1268.   return;
  1269. }
  1270.  
  1271. /* Move CHILD's pointers to the next command for it to execute.
  1272.    Returns nonzero if there is another command.  */
  1273.  
  1274. static int
  1275. job_next_command (child)
  1276.      struct child *child;
  1277. {
  1278.   while (child->command_ptr == 0 || *child->command_ptr == '\0')
  1279.     {
  1280.       /* There are no more lines in the expansion of this line.  */
  1281.       if (child->command_line == child->file->cmds->ncommand_lines)
  1282.     {
  1283.       /* There are no more lines to be expanded.  */
  1284.       child->command_ptr = 0;
  1285.       return 0;
  1286.     }
  1287.       else
  1288.     /* Get the next line to run.  */
  1289.     child->command_ptr = child->command_lines[child->command_line++];
  1290.     }
  1291.   return 1;
  1292. }
  1293.  
  1294. static int
  1295. load_too_high ()
  1296. {
  1297. #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA)
  1298.   return 1;
  1299. #else
  1300.   double load;
  1301.  
  1302.   if (max_load_average < 0)
  1303.     return 0;
  1304.  
  1305.   make_access ();
  1306.   if (getloadavg (&load, 1) != 1)
  1307.     {
  1308.       static int lossage = -1;
  1309.       /* Complain only once for the same error.  */
  1310.       if (lossage == -1 || errno != lossage)
  1311.     {
  1312.       if (errno == 0)
  1313.         /* An errno value of zero means getloadavg is just unsupported.  */
  1314.         error ("cannot enforce load limits on this operating system");
  1315.       else
  1316.         perror_with_name ("cannot enforce load limit: ", "getloadavg");
  1317.     }
  1318.       lossage = errno;
  1319.       load = 0;
  1320.     }
  1321.   user_access ();
  1322.  
  1323.   return load >= max_load_average;
  1324. #endif
  1325. }
  1326.  
  1327. /* Start jobs that are waiting for the load to be lower.  */
  1328.  
  1329. void
  1330. start_waiting_jobs ()
  1331. {
  1332.   struct child *job;
  1333.  
  1334.   if (waiting_jobs == 0)
  1335.     return;
  1336.  
  1337.   do
  1338.     {
  1339.       /* Check for recently deceased descendants.  */
  1340.       reap_children (0, 0);
  1341.  
  1342.       /* Take a job off the waiting list.  */
  1343.       job = waiting_jobs;
  1344.       waiting_jobs = job->next;
  1345.  
  1346.       /* Try to start that job.  We break out of the loop as soon
  1347.      as start_waiting_job puts one back on the waiting list.  */
  1348.     }
  1349.   while (start_waiting_job (job) && waiting_jobs != 0);
  1350.  
  1351.   return;
  1352. }
  1353.  
  1354. #ifndef WINDOWS32
  1355. #ifdef VMS
  1356. #include <descrip.h>
  1357. #include <clidef.h>
  1358.  
  1359. /* This is called as an AST when a child process dies (it won't get
  1360.    interrupted by anything except a higher level AST).
  1361. */
  1362. int vmsHandleChildTerm(struct child *child)
  1363. {
  1364.     int status;
  1365.     register struct child *lastc, *c;
  1366.     int child_failed;
  1367.  
  1368.     vms_jobsefnmask &= ~(1 << (child->efn - 32));
  1369.  
  1370.     lib$free_ef(&child->efn);
  1371.  
  1372.     (void) sigblock (fatal_signal_mask);
  1373.  
  1374.     child_failed = !(child->cstatus & 1 || ((child->cstatus & 7) == 0));
  1375.  
  1376.     /* Search for a child matching the deceased one.  */
  1377.     lastc = 0;
  1378. #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
  1379.     for (c = children; c != 0 && c != child; lastc = c, c = c->next);
  1380. #else
  1381.     c = child;
  1382. #endif
  1383.  
  1384.     if (child_failed && !c->noerror && !ignore_errors_flag)
  1385.       {
  1386.     /* The commands failed.  Write an error message,
  1387.        delete non-precious targets, and abort.  */
  1388.     child_error (c->file->name, c->cstatus, 0, 0, 0);
  1389.     c->file->update_status = 1;
  1390.     delete_child_targets (c);
  1391.       }
  1392.     else
  1393.       {
  1394.     if (child_failed)
  1395.       {
  1396.         /* The commands failed, but we don't care.  */
  1397.         child_error (c->file->name, c->cstatus, 0, 0, 1);
  1398.         child_failed = 0;
  1399.       }
  1400.  
  1401. #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
  1402.     /* If there are more commands to run, try to start them.  */
  1403.     start_job (c);
  1404.  
  1405.     switch (c->file->command_state)
  1406.       {
  1407.       case cs_running:
  1408.         /* Successfully started.  */
  1409.         break;
  1410.  
  1411.       case cs_finished:
  1412.         if (c->file->update_status != 0) {
  1413.         /* We failed to start the commands.  */
  1414.         delete_child_targets (c);
  1415.         }
  1416.         break;
  1417.  
  1418.       default:
  1419.         error ("internal error: `%s' command_state \
  1420. %d in child_handler", c->file->name);
  1421.         abort ();
  1422.         break;
  1423.       }
  1424. #endif /* RECURSIVEJOBS */
  1425.       }
  1426.  
  1427.     /* Set the state flag to say the commands have finished.  */
  1428.     c->file->command_state = cs_finished;
  1429.     notice_finished_file (c->file);
  1430.  
  1431. #if defined(RECURSIVEJOBS) /* I've had problems with recursive stuff and process handling */
  1432.     /* Remove the child from the chain and free it.  */
  1433.     if (lastc == 0)
  1434.       children = c->next;
  1435.     else
  1436.       lastc->next = c->next;
  1437.     free_child (c);
  1438. #endif /* RECURSIVEJOBS */
  1439.  
  1440.     /* There is now another slot open.  */
  1441.     if (job_slots_used > 0)
  1442.       --job_slots_used;
  1443.  
  1444.     /* If the job failed, and the -k flag was not given, die.  */
  1445.     if (child_failed && !keep_going_flag)
  1446.       die (EXIT_FAILURE);
  1447.  
  1448.     (void) sigsetmask (sigblock (0) & ~(fatal_signal_mask));
  1449.  
  1450.     return 1;
  1451. }
  1452.  
  1453. /* VMS:
  1454.    Spawn a process executing the command in ARGV and return its pid. */
  1455.  
  1456. #define MAXCMDLEN 200
  1457.  
  1458. int
  1459. child_execute_job (argv, child)
  1460.      char *argv;
  1461.      struct child *child;
  1462. {
  1463.   int i;
  1464.   static struct dsc$descriptor_s cmddsc;
  1465. #ifndef DONTWAITFORCHILD
  1466.   int spflags = 0;
  1467. #else
  1468.   int spflags = CLI$M_NOWAIT;
  1469. #endif
  1470.   int status;
  1471.   char cmd[4096],*p,*c;
  1472.   char comname[50];
  1473.  
  1474. /* Remove backslashes */
  1475.   for (p = argv, c = cmd; *p; p++,c++)
  1476.     {
  1477.       if (*p == '\\') p++;
  1478.     *c = *p;
  1479.     }
  1480.   *c = *p;
  1481.  
  1482.   /* check for maximum dcl length and create *.com file if neccesary */
  1483.  
  1484.   comname[0] = '\0';
  1485.  
  1486.   if (strlen (cmd) > MAXCMDLEN)
  1487.     {
  1488.       FILE *outfile;
  1489.       char tmp;
  1490.  
  1491.       strcpy (comname, "sys$scratch:CMDXXXXXX.COM");
  1492.       (void) mktemp (comname);
  1493.  
  1494.       outfile = fopen (comname, "w");
  1495.       if (outfile == 0)
  1496.     pfatal_with_name (comname);
  1497.  
  1498.       fprintf (outfile, "$ ");
  1499.       c = cmd;
  1500.  
  1501.       while (c)
  1502.     {
  1503.       p = strchr (c, ',');
  1504.       if ((p == NULL) || (p-c > MAXCMDLEN))
  1505.         p = strchr (c, ' ');
  1506.       if (p != NULL)
  1507.         {
  1508.           p++;
  1509.           tmp = *p;
  1510.           *p = '\0';
  1511.         }
  1512.       else
  1513.         tmp = '\0';
  1514.       fprintf (outfile, "%s%s\n", c, (tmp == '\0')?"":" -");
  1515.       if (p != NULL)
  1516.         *p = tmp;
  1517.       c = p;
  1518.     }
  1519.  
  1520.       fclose (outfile);
  1521.  
  1522.       sprintf (cmd, "$ @%s", comname);
  1523.  
  1524.       if (debug_flag)
  1525.     printf ("Executing %s instead\n", cmd);
  1526.     }
  1527.  
  1528.   cmddsc.dsc$w_length = strlen(cmd);
  1529.   cmddsc.dsc$a_pointer = cmd;
  1530.   cmddsc.dsc$b_dtype = DSC$K_DTYPE_T;
  1531.   cmddsc.dsc$b_class = DSC$K_CLASS_S;
  1532.  
  1533.   child->efn = 0;
  1534.   while (child->efn < 32 || child->efn > 63)
  1535.     {
  1536.       status = lib$get_ef(&child->efn);
  1537.       if (!(status & 1))
  1538.     return 0;
  1539.     }
  1540.  
  1541.   sys$clref(child->efn);
  1542.  
  1543.   vms_jobsefnmask |= (1 << (child->efn - 32));
  1544.  
  1545. #ifndef DONTWAITFORCHILD
  1546.   status = lib$spawn(&cmddsc,0,0,&spflags,0,&child->pid,&child->cstatus,
  1547.                &child->efn,0,0);
  1548.   vmsHandleChildTerm(child);
  1549. #else
  1550.   status = lib$spawn(&cmddsc,0,0,&spflags,0,&child->pid,&child->cstatus,
  1551.                &child->efn,vmsHandleChildTerm,child);
  1552. #endif
  1553.  
  1554.   if (!(status & 1))
  1555.     {
  1556.       printf("Error spawning, %d\n",status);
  1557.       fflush(stdout);
  1558.     }
  1559.  
  1560.   unlink (comname);
  1561.  
  1562.   return (status & 1);
  1563. }
  1564.  
  1565. #else /* !VMS */
  1566.  
  1567. #if !defined (_AMIGA) && !defined (__MSDOS__)
  1568. /* UNIX:
  1569.    Replace the current process with one executing the command in ARGV.
  1570.    STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
  1571.    the environment of the new program.  This function does not return.  */
  1572.  
  1573. void
  1574. child_execute_job (stdin_fd, stdout_fd, argv, envp)
  1575.      int stdin_fd, stdout_fd;
  1576.      char **argv, **envp;
  1577. {
  1578.   if (stdin_fd != 0)
  1579.     (void) dup2 (stdin_fd, 0);
  1580.   if (stdout_fd != 1)
  1581.     (void) dup2 (stdout_fd, 1);
  1582.   if (stdin_fd != 0)
  1583.     (void) close (stdin_fd);
  1584.   if (stdout_fd != 1)
  1585.     (void) close (stdout_fd);
  1586.  
  1587.   /* Run the command.  */
  1588.   exec_command (argv, envp);
  1589. }
  1590. #endif /* !AMIGA && !__MSDOS__ */
  1591. #endif /* !VMS */
  1592. #endif /* !WINDOWS32 */
  1593.  
  1594. #ifndef _AMIGA
  1595. /* Replace the current process with one running the command in ARGV,
  1596.    with environment ENVP.  This function does not return.  */
  1597.  
  1598. void
  1599. exec_command (argv, envp)
  1600.      char **argv, **envp;
  1601. {
  1602. #ifdef VMS
  1603.   /* Run the program.  */
  1604.   execve (argv[0], argv, envp);
  1605.   perror_with_name ("execve: ", argv[0]);
  1606.   _exit (EXIT_FAILURE);
  1607. #else
  1608. #ifdef WINDOWS32
  1609.   HANDLE hPID;
  1610.   HANDLE hWaitPID;
  1611.   int err = 0;
  1612.   int exit_code = EXIT_FAILURE;
  1613.  
  1614.   /* make sure CreateProcess() has Path it needs */
  1615.   sync_Path_environment();
  1616.  
  1617.   /* launch command */
  1618.   hPID = process_easy(argv, envp);
  1619.  
  1620.   /* make sure launch ok */
  1621.   if (hPID == INVALID_HANDLE_VALUE)
  1622.     {
  1623.       int i;
  1624.       fprintf(stderr,
  1625.               "process_easy() failed failed to launch process (e=%d)\n",
  1626.               process_last_err(hPID));
  1627.       for (i = 0; argv[i]; i++)
  1628.           fprintf(stderr, "%s ", argv[i]);
  1629.       fprintf(stderr, "\nCounted %d args in failed launch\n", i);
  1630.       exit(EXIT_FAILURE);
  1631.     }
  1632.  
  1633.   /* wait and reap last child */
  1634.   while (hWaitPID = process_wait_for_any())
  1635.     {
  1636.       /* was an error found on this process? */
  1637.       err = process_last_err(hWaitPID);
  1638.  
  1639.       /* get exit data */
  1640.       exit_code = process_exit_code(hWaitPID);
  1641.  
  1642.       if (err)
  1643.           fprintf(stderr, "make (e=%d, rc=%d): %s",
  1644.                   err, exit_code, map_windows32_error_to_string(err));
  1645.  
  1646.       /* cleanup process */
  1647.       process_cleanup(hWaitPID);
  1648.  
  1649.       /* expect to find only last pid, warn about other pids reaped */
  1650.       if (hWaitPID == hPID)
  1651.           break;
  1652.       else
  1653.           fprintf(stderr,
  1654.                   "make reaped child pid %d, still waiting for pid %d\n",
  1655.                   hWaitPID, hPID);
  1656.     }
  1657.  
  1658.   /* return child's exit code as our exit code */
  1659.   exit(exit_code);
  1660.  
  1661. #else  /* !WINDOWS32 */
  1662.  
  1663.   /* Be the user, permanently.  */
  1664.   child_access ();
  1665.  
  1666.   /* Run the program.  */
  1667.   environ = envp;
  1668.   execvp (argv[0], argv);
  1669.  
  1670.   switch (errno)
  1671.     {
  1672.     case ENOENT:
  1673.       error ("%s: Command not found", argv[0]);
  1674.       break;
  1675.     case ENOEXEC:
  1676.       {
  1677.     /* The file is not executable.  Try it as a shell script.  */
  1678.     extern char *getenv ();
  1679.     char *shell;
  1680.     char **new_argv;
  1681.     int argc;
  1682.  
  1683.     shell = getenv ("SHELL");
  1684.     if (shell == 0)
  1685.       shell = default_shell;
  1686.  
  1687.     argc = 1;
  1688.     while (argv[argc] != 0)
  1689.       ++argc;
  1690.  
  1691.     new_argv = (char **) alloca ((1 + argc + 1) * sizeof (char *));
  1692.     new_argv[0] = shell;
  1693.     new_argv[1] = argv[0];
  1694.     while (argc > 0)
  1695.       {
  1696.         new_argv[1 + argc] = argv[argc];
  1697.         --argc;
  1698.       }
  1699.  
  1700.     execvp (shell, new_argv);
  1701.     if (errno == ENOENT)
  1702.       error ("%s: Shell program not found", shell);
  1703.     else
  1704.       perror_with_name ("execvp: ", shell);
  1705.     break;
  1706.       }
  1707.  
  1708.     default:
  1709.       perror_with_name ("execvp: ", argv[0]);
  1710.       break;
  1711.     }
  1712.  
  1713.   _exit (127);
  1714. #endif /* !WINDOWS32 */
  1715. #endif /* !VMS */
  1716. }
  1717. #else /* On Amiga */
  1718. void exec_command (argv)
  1719.      char **argv;
  1720. {
  1721.   MyExecute (argv);
  1722. }
  1723.  
  1724. void clean_tmp (void)
  1725. {
  1726.   DeleteFile (amiga_bname);
  1727. }
  1728.  
  1729. #endif /* On Amiga */
  1730.  
  1731. #ifndef VMS
  1732. /* Figure out the argument list necessary to run LINE as a command.  Try to
  1733.    avoid using a shell.  This routine handles only ' quoting, and " quoting
  1734.    when no backslash, $ or ` characters are seen in the quotes.  Starting
  1735.    quotes may be escaped with a backslash.  If any of the characters in
  1736.    sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
  1737.    is the first word of a line, the shell is used.
  1738.  
  1739.    If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
  1740.    If *RESTP is NULL, newlines will be ignored.
  1741.  
  1742.    SHELL is the shell to use, or nil to use the default shell.
  1743.    IFS is the value of $IFS, or nil (meaning the default).  */
  1744.  
  1745. static char **
  1746. construct_command_argv_internal (line, restp, shell, ifs)
  1747.      char *line, **restp;
  1748.      char *shell, *ifs;
  1749. {
  1750. #ifdef __MSDOS__
  1751.   /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
  1752.      We call `system' for anything that requires ``slow'' processing,
  1753.      because DOS shells are too dumb.  When $SHELL points to a real
  1754.      (unix-style) shell, `system' just calls it to do everything.  When
  1755.      $SHELL points to a DOS shell, `system' does most of the work
  1756.      internally, calling the shell only for its internal commands.
  1757.      However, it looks on the $PATH first, so you can e.g. have an
  1758.      external command named `mkdir'.
  1759.  
  1760.      Since we call `system', certain characters and commands below are
  1761.      actually not specific to COMMAND.COM, but to the DJGPP implementation
  1762.      of `system'.  In particular:
  1763.  
  1764.        The shell wildcard characters are in DOS_CHARS because they will
  1765.        not be expanded if we call the child via `spawnXX'.
  1766.  
  1767.        The `;' is in DOS_CHARS, because our `system' knows how to run
  1768.        multiple commands on a single line.
  1769.  
  1770.        DOS_CHARS also include characters special to 4DOS/NDOS, so we
  1771.        won't have to tell one from another and have one more set of
  1772.        commands and special characters.  */
  1773.   static char sh_chars_dos[] = "*?[];|<>%^&()";
  1774.   static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
  1775.                  "copy", "ctty", "date", "del", "dir", "echo",
  1776.                  "erase", "exit", "for", "goto", "if", "md",
  1777.                  "mkdir", "path", "pause", "prompt", "rd",
  1778.                  "rmdir", "rem", "ren", "rename", "set",
  1779.                  "shift", "time", "type", "ver", "verify",
  1780.                  "vol", ":", 0 };
  1781.  
  1782.   static char sh_chars_sh[]  = "#;\"*?[]&|<>(){}$`^";
  1783.   static char *sh_cmds_sh[]  = { "cd", "eval", "exec", "exit", "login",
  1784.                  "logout", "set", "umask", "wait", "while",
  1785.                  "for", "case", "if", ":", ".", "break",
  1786.                  "continue", "export", "read", "readonly",
  1787.                  "shift", "times", "trap", "switch", 0 };
  1788.  
  1789.   char *sh_chars;
  1790.   char **sh_cmds;
  1791. #else
  1792. #ifdef _AMIGA
  1793.   static char sh_chars[] = "#;\"|<>()?*$`";
  1794.   static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
  1795.                  "rename", "set", "setenv", "date", "makedir",
  1796.                  "skip", "else", "endif", "path", "prompt",
  1797.                  "unset", "unsetenv", "version",
  1798.                  0 };
  1799. #else
  1800. #ifdef WINDOWS32
  1801.   static char sh_chars_dos[] = "\"|<>";
  1802.   static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
  1803.                  "copy", "ctty", "date", "del", "dir", "echo",
  1804.                  "erase", "exit", "for", "goto", "if", "if", "md",
  1805.                  "mkdir", "path", "pause", "prompt", "rem", "ren",
  1806.                  "rename", "set", "shift", "time", "type",
  1807.                  "ver", "verify", "vol", ":", 0 };
  1808.   static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
  1809.   static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
  1810.                  "logout", "set", "umask", "wait", "while", "for",
  1811.                  "case", "if", ":", ".", "break", "continue",
  1812.                  "export", "read", "readonly", "shift", "times",
  1813.                  "trap", "switch", "test", 0 };
  1814.   char*  sh_chars;
  1815.   char** sh_cmds;
  1816. #else  /* WINDOWS32 */
  1817.   static char sh_chars[] = "#;\"*?[]&|<>(){}$`^";
  1818.   static char *sh_cmds[] = { "cd", "eval", "exec", "exit", "login",
  1819.                  "logout", "set", "umask", "wait", "while", "for",
  1820.                  "case", "if", ":", ".", "break", "continue",
  1821.                  "export", "read", "readonly", "shift", "times",
  1822.                  "trap", "switch", 0 };
  1823. #endif /* WINDOWS32 */
  1824. #endif /* Amiga */
  1825. #endif /* __MSDOS__ */
  1826.   register int i;
  1827.   register char *p;
  1828.   register char *ap;
  1829.   char *end;
  1830.   int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
  1831.   char **new_argv = 0;
  1832. #ifdef WINDOWS32
  1833.   int slow_flag = 0;
  1834.  
  1835.   if (no_default_sh_exe) {
  1836.     sh_cmds = sh_cmds_dos;
  1837.     sh_chars = sh_chars_dos;
  1838.   } else {
  1839.     sh_cmds = sh_cmds_sh;
  1840.     sh_chars = sh_chars_sh;
  1841.   }
  1842. #endif /* WINDOWS32 */
  1843.  
  1844.   if (restp != NULL)
  1845.     *restp = NULL;
  1846.  
  1847.   /* Make sure not to bother processing an empty line.  */
  1848.   while (isblank (*line))
  1849.     ++line;
  1850.   if (*line == '\0')
  1851.     return 0;
  1852.  
  1853.   /* See if it is safe to parse commands internally.  */
  1854.   if (shell == 0)
  1855.     shell = default_shell;
  1856. #ifdef WINDOWS32
  1857.   else if (strcmp (shell, default_shell))
  1858.   {
  1859.     char *s1 = _fullpath(NULL, shell, 0);
  1860.     char *s2 = _fullpath(NULL, default_shell, 0);
  1861.  
  1862.     slow_flag = strcmp((s1 ? s1 : ""), (s2 ? s2 : ""));
  1863.  
  1864.     if (s1);
  1865.       free(s1);
  1866.     if (s2);
  1867.       free(s2);
  1868.   }
  1869.   if (slow_flag)
  1870.     goto slow;
  1871. #else  /* not WINDOWS32 */
  1872. #ifdef __MSDOS__
  1873.   else if (stricmp (shell, default_shell))
  1874.     {
  1875.       extern int _is_unixy_shell (const char *_path);
  1876.  
  1877.       message (1, "$SHELL changed (was `%s', now `%s')", default_shell, shell);
  1878.       unixy_shell = _is_unixy_shell (shell);
  1879.       default_shell = shell;
  1880.     }
  1881.   if (unixy_shell)
  1882.     {
  1883.       sh_chars = sh_chars_sh;
  1884.       sh_cmds  = sh_cmds_sh;
  1885.     }
  1886.   else
  1887.     {
  1888.       sh_chars = sh_chars_dos;
  1889.       sh_cmds  = sh_cmds_dos;
  1890.     }
  1891. #else  /* not __MSDOS__ */
  1892.   else if (strcmp (shell, default_shell))
  1893.     goto slow;
  1894. #endif /* not __MSDOS__ */
  1895. #endif /* not WINDOWS32 */
  1896.  
  1897.   if (ifs != 0)
  1898.     for (ap = ifs; *ap != '\0'; ++ap)
  1899.       if (*ap != ' ' && *ap != '\t' && *ap != '\n')
  1900.     goto slow;
  1901.  
  1902.   i = strlen (line) + 1;
  1903.  
  1904.   /* More than 1 arg per character is impossible.  */
  1905.   new_argv = (char **) xmalloc (i * sizeof (char *));
  1906.  
  1907.   /* All the args can fit in a buffer as big as LINE is.   */
  1908.   ap = new_argv[0] = (char *) xmalloc (i);
  1909.   end = ap + i;
  1910.  
  1911.   /* I is how many complete arguments have been found.  */
  1912.   i = 0;
  1913.   instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
  1914.   for (p = line; *p != '\0'; ++p)
  1915.     {
  1916.       if (ap > end)
  1917.     abort ();
  1918.  
  1919.       if (instring)
  1920.     {
  1921.     string_char:
  1922.       /* Inside a string, just copy any char except a closing quote
  1923.          or a backslash-newline combination.  */
  1924.       if (*p == instring)
  1925.         {
  1926.           instring = 0;
  1927.           if (ap == new_argv[0] || *(ap-1) == '\0')
  1928.         last_argument_was_empty = 1;
  1929.         }
  1930.       else if (*p == '\\' && p[1] == '\n')
  1931.         goto swallow_escaped_newline;
  1932.       else if (*p == '\n' && restp != NULL)
  1933.         {
  1934.           /* End of the command line.  */
  1935.           *restp = p;
  1936.           goto end_of_line;
  1937.         }
  1938.       /* Backslash, $, and ` are special inside double quotes.
  1939.          If we see any of those, punt.
  1940.          But on MSDOS, if we use COMMAND.COM, double and single
  1941.          quotes have the same effect.  */
  1942.       else if (instring == '"' && index ("\\$`", *p) != 0 && unixy_shell)
  1943.         goto slow;
  1944.       else
  1945.         *ap++ = *p;
  1946.     }
  1947.       else if (index (sh_chars, *p) != 0)
  1948.     /* Not inside a string, but it's a special char.  */
  1949.     goto slow;
  1950. #ifdef  __MSDOS__
  1951.       else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
  1952.     /* `...' is a wildcard in DJGPP.  */
  1953.     goto slow;
  1954. #endif
  1955.       else
  1956.     /* Not a special char.  */
  1957.     switch (*p)
  1958.       {
  1959.       case '=':
  1960.         /* Equals is a special character in leading words before the
  1961.            first word with no equals sign in it.  This is not the case
  1962.            with sh -k, but we never get here when using nonstandard
  1963.            shell flags.  */
  1964.         if (! seen_nonequals && unixy_shell)
  1965.           goto slow;
  1966.         word_has_equals = 1;
  1967.         *ap++ = '=';
  1968.         break;
  1969.  
  1970.       case '\\':
  1971.         /* Backslash-newline combinations are eaten.  */
  1972.         if (p[1] == '\n')
  1973.           {
  1974.           swallow_escaped_newline:
  1975.  
  1976.         /* Eat the backslash, the newline, and following whitespace,
  1977.            replacing it all with a single space.  */
  1978.         p += 2;
  1979.  
  1980.         /* If there is a tab after a backslash-newline,
  1981.            remove it from the source line which will be echoed,
  1982.            since it was most likely used to line
  1983.            up the continued line with the previous one.  */
  1984.         if (*p == '\t')
  1985.           strcpy (p, p + 1);
  1986.  
  1987.         if (instring)
  1988.           goto string_char;
  1989.         else
  1990.           {
  1991.             if (ap != new_argv[i])
  1992.               /* Treat this as a space, ending the arg.
  1993.              But if it's at the beginning of the arg, it should
  1994.              just get eaten, rather than becoming an empty arg. */
  1995.               goto end_of_arg;
  1996.             else
  1997.               p = next_token (p) - 1;
  1998.           }
  1999.           }
  2000.         else if (p[1] != '\0')
  2001.           /* Copy and skip the following char.  */
  2002.           *ap++ = *++p;
  2003.         break;
  2004.  
  2005.       case '\'':
  2006.       case '"':
  2007.         instring = *p;
  2008.         break;
  2009.  
  2010.       case '\n':
  2011.         if (restp != NULL)
  2012.           {
  2013.         /* End of the command line.  */
  2014.         *restp = p;
  2015.         goto end_of_line;
  2016.           }
  2017.         else
  2018.           /* Newlines are not special.  */
  2019.           *ap++ = '\n';
  2020.         break;
  2021.  
  2022.       case ' ':
  2023.       case '\t':
  2024.       end_of_arg:
  2025.         /* We have the end of an argument.
  2026.            Terminate the text of the argument.  */
  2027.         *ap++ = '\0';
  2028.         new_argv[++i] = ap;
  2029.         last_argument_was_empty = 0;
  2030.  
  2031.         /* Update SEEN_NONEQUALS, which tells us if every word
  2032.            heretofore has contained an `='.  */
  2033.         seen_nonequals |= ! word_has_equals;
  2034.         if (word_has_equals && ! seen_nonequals)
  2035.           /* An `=' in a word before the first
  2036.          word without one is magical.  */
  2037.           goto slow;
  2038.         word_has_equals = 0; /* Prepare for the next word.  */
  2039.  
  2040.         /* If this argument is the command name,
  2041.            see if it is a built-in shell command.
  2042.            If so, have the shell handle it.  */
  2043.         if (i == 1)
  2044.           {
  2045.         register int j;
  2046.         for (j = 0; sh_cmds[j] != 0; ++j)
  2047.           if (streq (sh_cmds[j], new_argv[0]))
  2048.             goto slow;
  2049.           }
  2050.  
  2051.         /* Ignore multiple whitespace chars.  */
  2052.         p = next_token (p);
  2053.         /* Next iteration should examine the first nonwhite char.  */
  2054.         --p;
  2055.         break;
  2056.  
  2057.       default:
  2058.         *ap++ = *p;
  2059.         break;
  2060.       }
  2061.     }
  2062.  end_of_line:
  2063.  
  2064.   if (instring)
  2065.     /* Let the shell deal with an unterminated quote.  */
  2066.     goto slow;
  2067.  
  2068.   /* Terminate the last argument and the argument list.  */
  2069.  
  2070.   *ap = '\0';
  2071.   if (new_argv[i][0] != '\0' || last_argument_was_empty)
  2072.     ++i;
  2073.   new_argv[i] = 0;
  2074.  
  2075.   if (i == 1)
  2076.     {
  2077.       register int j;
  2078.       for (j = 0; sh_cmds[j] != 0; ++j)
  2079.     if (streq (sh_cmds[j], new_argv[0]))
  2080.       goto slow;
  2081.     }
  2082.  
  2083.   if (new_argv[0] == 0)
  2084.     /* Line was empty.  */
  2085.     return 0;
  2086.   else
  2087.     return new_argv;
  2088.  
  2089.  slow:;
  2090.   /* We must use the shell.  */
  2091.  
  2092.   if (new_argv != 0)
  2093.     {
  2094.       /* Free the old argument list we were working on.  */
  2095.       free (new_argv[0]);
  2096.       free ((void *)new_argv);
  2097.     }
  2098.  
  2099. #ifdef __MSDOS__
  2100.   execute_by_shell = 1;    /* actually, call `system' if shell isn't unixy */
  2101. #endif
  2102.  
  2103. #ifdef _AMIGA
  2104.   {
  2105.     char *ptr;
  2106.     char *buffer;
  2107.     char *dptr;
  2108.  
  2109.     buffer = (char *)xmalloc (strlen (line)+1);
  2110.  
  2111.     ptr = line;
  2112.     for (dptr=buffer; *ptr; )
  2113.     {
  2114.       if (*ptr == '\\' && ptr[1] == '\n')
  2115.     ptr += 2;
  2116.       else if (*ptr == '@') /* Kludge: multiline commands */
  2117.       {
  2118.     ptr += 2;
  2119.     *dptr++ = '\n';
  2120.       }
  2121.       else
  2122.     *dptr++ = *ptr++;
  2123.     }
  2124.     *dptr = 0;
  2125.  
  2126.     new_argv = (char **) xmalloc(2 * sizeof(char *));
  2127.     new_argv[0] = buffer;
  2128.     new_argv[1] = 0;
  2129.   }
  2130. #else    /* Not Amiga  */
  2131. #ifdef WINDOWS32
  2132.   /*
  2133.    * Not eating this whitespace caused things like
  2134.    *
  2135.    *    sh -c "\n"
  2136.    *
  2137.    * which gave the shell fits. I think we have to eat
  2138.    * whitespace here, but this code should be considered
  2139.    * suspicious if things start failing....
  2140.    */
  2141.  
  2142.   /* Make sure not to bother processing an empty line.  */
  2143.   while (isspace (*line))
  2144.     ++line;
  2145.   if (*line == '\0')
  2146.     return 0;
  2147.  
  2148.   /*
  2149.    * only come here if no sh.exe command
  2150.    */
  2151.   if (no_default_sh_exe)
  2152.   {
  2153.     FILE *batch;
  2154.     dos_batch_file = 1;
  2155.     if (dos_bname == 0)
  2156.     {
  2157.       dos_bname = tempnam (".", "mk");
  2158.       for (i = 0; dos_bname[i] != '\0'; ++i)
  2159.     if (dos_bname[i] == '/')
  2160.       dos_bname[i] = '\\';
  2161.       dos_bename = (char *) xmalloc (strlen (dos_bname) + 5);
  2162.       strcpy (dos_bename, dos_bname);
  2163.       strcat (dos_bname, ".bat");
  2164.       strcat (dos_bename, ".err");
  2165.     }
  2166.     batch = fopen (dos_bename, "w"); /* Create a file.  */
  2167.     if (batch != NULL)
  2168.       fclose (batch);
  2169.     batch = fopen (dos_bname, "w");
  2170.     fputs ("@echo off\n", batch);
  2171.     fputs (line, batch);
  2172.     fprintf (batch, "\nif errorlevel 1 del %s\n", dos_bename);
  2173.     fclose (batch);
  2174.     new_argv = (char **) xmalloc(2 * sizeof(char *));
  2175.     new_argv[0] = strdup (dos_bname);
  2176.     new_argv[1] = 0;
  2177.   }
  2178.   else
  2179. #endif /* WINDOWS32 */
  2180.   {
  2181.     /* SHELL may be a multi-word command.  Construct a command line
  2182.        "SHELL -c LINE", with all special chars in LINE escaped.
  2183.        Then recurse, expanding this command line to get the final
  2184.        argument list.  */
  2185.  
  2186.     unsigned int shell_len = strlen (shell);
  2187.     static char minus_c[] = " -c ";
  2188.     unsigned int line_len = strlen (line);
  2189.  
  2190.     char *new_line = (char *) alloca (shell_len + (sizeof (minus_c) - 1)
  2191.                       + (line_len * 2) + 1);
  2192.  
  2193.     ap = new_line;
  2194.     bcopy (shell, ap, shell_len);
  2195.     ap += shell_len;
  2196.     bcopy (minus_c, ap, sizeof (minus_c) - 1);
  2197.     ap += sizeof (minus_c) - 1;
  2198.     for (p = line; *p != '\0'; ++p)
  2199.       {
  2200.     if (restp != NULL && *p == '\n')
  2201.       {
  2202.         *restp = p;
  2203.         break;
  2204.       }
  2205.     else if (*p == '\\' && p[1] == '\n')
  2206.       {
  2207.         /* Eat the backslash, the newline, and following whitespace,
  2208.            replacing it all with a single space (which is escaped
  2209.            from the shell).  */
  2210.         p += 2;
  2211.  
  2212.         /* If there is a tab after a backslash-newline,
  2213.            remove it from the source line which will be echoed,
  2214.            since it was most likely used to line
  2215.            up the continued line with the previous one.  */
  2216.         if (*p == '\t')
  2217.           bcopy (p + 1, p, strlen (p));
  2218.  
  2219.         p = next_token (p);
  2220.         --p;
  2221.             if (unixy_shell)
  2222.               *ap++ = '\\';
  2223.         *ap++ = ' ';
  2224.         continue;
  2225.       }
  2226.  
  2227.         /* DOS shells don't know about backslash-escaping.  */
  2228.     if (unixy_shell &&
  2229.             (*p == '\\' || *p == '\'' || *p == '"'
  2230.              || isspace (*p)
  2231.              || index (sh_chars, *p) != 0))
  2232.       *ap++ = '\\';
  2233. #ifdef __MSDOS__
  2234.         else if (unixy_shell && strncmp (p, "...", 3) == 0)
  2235.           {
  2236.             /* The case of `...' wildcard again.  */
  2237.             strcpy (ap, "\\.\\.\\");
  2238.             ap += 5;
  2239.             p  += 2;
  2240.           }
  2241. #endif
  2242.     *ap++ = *p;
  2243.       }
  2244.     if (ap == new_line + shell_len + sizeof (minus_c) - 1)
  2245.       /* Line was empty.  */
  2246.       return 0;
  2247.     *ap = '\0';
  2248.  
  2249.     if (unixy_shell)
  2250.       new_argv = construct_command_argv_internal (new_line, (char **) NULL,
  2251.                                                   (char *) 0, (char *) 0);
  2252. #ifdef  __MSDOS__
  2253.     else
  2254.       {
  2255.       /* With MSDOS shells, we must construct the command line here
  2256.          instead of recursively calling ourselves, because we
  2257.          cannot backslash-escape the special characters (see above).  */
  2258.       new_argv = (char **) xmalloc (sizeof (char *));
  2259.       line_len = strlen (new_line) - shell_len - sizeof (minus_c) + 1;
  2260.       new_argv[0] = xmalloc (line_len + 1);
  2261.       strncpy (new_argv[0],
  2262.                new_line + shell_len + sizeof (minus_c) - 1, line_len);
  2263.       new_argv[0][line_len] = '\0';
  2264.       }
  2265. #endif
  2266.   }
  2267. #endif    /* ! AMIGA */
  2268.  
  2269.   return new_argv;
  2270. }
  2271.  
  2272. /* Figure out the argument list necessary to run LINE as a command.  Try to
  2273.    avoid using a shell.  This routine handles only ' quoting, and " quoting
  2274.    when no backslash, $ or ` characters are seen in the quotes.  Starting
  2275.    quotes may be escaped with a backslash.  If any of the characters in
  2276.    sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
  2277.    is the first word of a line, the shell is used.
  2278.  
  2279.    If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
  2280.    If *RESTP is NULL, newlines will be ignored.
  2281.  
  2282.    FILE is the target whose commands these are.  It is used for
  2283.    variable expansion for $(SHELL) and $(IFS).  */
  2284.  
  2285. char **
  2286. construct_command_argv (line, restp, file)
  2287.      char *line, **restp;
  2288.      struct file *file;
  2289. {
  2290.   char *shell, *ifs;
  2291.   char **argv;
  2292.  
  2293.   {
  2294.     /* Turn off --warn-undefined-variables while we expand SHELL and IFS.  */
  2295.     int save = warn_undefined_variables_flag;
  2296.     warn_undefined_variables_flag = 0;
  2297.  
  2298.     shell = allocated_variable_expand_for_file ("$(SHELL)", file);
  2299. #ifdef WINDOWS32
  2300.     /*
  2301.      * Convert to forward slashes so that construct_command_argv_internal()
  2302.      * is not confused.
  2303.      */
  2304.     if (shell) {
  2305.       char *p = w32ify(shell, 0);
  2306.       strcpy(shell, p);
  2307.     }
  2308. #endif
  2309.     ifs = allocated_variable_expand_for_file ("$(IFS)", file);
  2310.  
  2311.     warn_undefined_variables_flag = save;
  2312.   }
  2313.  
  2314.   argv = construct_command_argv_internal (line, restp, shell, ifs);
  2315.  
  2316.   free (shell);
  2317.   free (ifs);
  2318.  
  2319.   return argv;
  2320. }
  2321. #endif /* !VMS */
  2322.  
  2323. #if !defined(HAVE_DUP2) && !defined(_AMIGA)
  2324. int
  2325. dup2 (old, new)
  2326.      int old, new;
  2327. {
  2328.   int fd;
  2329.  
  2330.   (void) close (new);
  2331.   fd = dup (old);
  2332.   if (fd != new)
  2333.     {
  2334.       (void) close (fd);
  2335.       errno = EMFILE;
  2336.       return -1;
  2337.     }
  2338.  
  2339.   return fd;
  2340. }
  2341. #endif /* !HAPE_DUP2 && !_AMIGA */
  2342.